

.LC0:
	.string "a.txt"

.LC1:
	.string "open file:a.txt fd = %d\n"

.LC2:
	.string "Hello World!\n"

.LC3:
	.string "size = %d\nread from file:\n %s\n"

.globl main

main:
	pushq %rbp
	movq %rsp,%rbp
	subq $0x1000,%rsp

	movl $0,-0x1000(%rbp) /* fd */
	movl $0,-0xffc(%rbp) /* len */
	movq $0,-0xff0(%rbp) /* buf  */
	movq $0,-0xfe0(%rbp) /* out */
	movl $0,-0xfd0(%rbp) /* size */

	movl $20,%edi 
	call malloc 
	movq %rax,-0xff0(%rbp) /* buf */

	leaq .LC2(%rip),%rsi
	movq -0xff0(%rbp),%rdi /* buf */
	call strcpy 

	movl $30,%edi
	call malloc 
	movq %rax,-0xfe0(%rbp) /* out */

	movl $0600,%edx
	movl $64,%eax /* O_CREAT 创建*/
	movl $512,%ebx /* O_TRUNC 删除*/
	orl %ebx,%eax
	movl $2,%ebx /* O_RDWR 读写*/
	orl %ebx,%eax 
	movl %eax,%esi
	leaq .LC0(%rip),%rdi 
	call open 
	movl %eax,-0x1000(%rbp) /* fd */

	movl -0x1000(%rbp),%esi /* fd */
	leaq .LC1(%rip),%rdi
	movl $0,%eax
	call printf 

	movq -0xff0(%rbp),%rdi /* buf */
	call strlen 
	movl %eax,-0xffc(%rbp) /* len */

	movl -0xffc(%rbp),%edx /* len */
	movq -0xff0(%rbp),%rsi /* buf */
	movl -0x1000(%rbp),%edi /* fd */
	call write 
	movl %eax,-0xfd0(%rbp) /* size */

	movl -0x1000(%rbp),%edi /* fd */
	call close 

	movl $0600,%edx
	movl $2,%esi 
	leaq .LC0(%rip),%rdi 
	call open 
	movl %eax,-0x1000(%rbp) /* fd */

	movl $0,%edx
	movl $0,%esi 
	movl -0x1000(%rbp),%edi  /* fd */
	call lseek 

	movl $12,%edx
	movq -0xfe0(%rbp),%rsi /* out */
	movl -0x1000(%rbp),%edi /* fd */
	call read 
	movl %eax,-0xfd0(%rbp) /* size */

	movq -0xfe0(%rbp),%rdx /* out */
	movl -0xfd0(%rbp),%esi /* size */
	leaq .LC3(%rip),%rdi
	movl $0,%eax 
	call printf 

	movl -0x1000(%rbp),%edi /* fd */
	call close 

	movq -0xff0(%rbp),%rdi /* buf */
	call free 

	movq -0xfe0(%rbp),%rdi /* out */
	call free 

	movl $0,%eax
	leave
	ret
	
